<?php

namespace App\Http\Controllers\LiveStream;

use App\Http\Controllers\Controller;
use App\Models\LiveBroadcastRoom;
use App\Models\RecLive;
use App\Services\Factory\UploadingFactory\UploadingFactory;
use App\Services\live_hill_seeding;
use App\Services\LiveC;
use Illuminate\Http\Request;


class LiveStreamController extends Controller
{
    protected $maxkeys = '1000';

    //生成推流拉流地址
    public function liveBroadcastUrl()
    {
        $res = new LiveC();
        $resultUrl = $res->getURl();
        return $resultUrl;
    }


    //图片云存储
    public function file(Request $request)
    {
        $img = $request->file('image');
        $uploading = UploadingFactory::uploading('Qiniu');
        $uploading->uploading($img);
    }

    //直播页面
    public function livePage(Request $request)
    {
        //直播间id
        $live_id = $request->get('id');
        $liveData = LiveBroadcastRoom::where('id', $live_id)->first()->toArray();
        if ($liveData['live_status'] != 1) {
            return redirect(route('liveError'));
        }
        return view('live.live_stream', ['play_url' => $liveData['play_url']]);
    }

    public function live2()
    {
        return view('live.live2');
    }

    //进入直播间失败入口
    public function liveError()
    {
        return '进入直播间失败';
    }

    public function liveVideo()
    {
        return view('live.live_video');
    }

    /**
     * 获取录播视频链接
     * @return array|void
     */
    public function hillSeeding()
    {
        $hillSeeding = new live_hill_seeding();
        $res = $hillSeeding->list_Objects('','' ,'', '');
        $url = [];
        foreach ($res as $k=>$v){
            $url[$k] = ['rec_live_url'=>$v];
        }
        try {
            foreach ($url as $v){
                RecLive::create($v);
                return ['code'=>500,'msg'=>'success','data'=>[]];
            }
        }catch (\Exception $e){
            return ['code'=>500,'error'=>$e,'data'=>[]];
        }
    }

    /**
     * 录播页面
     * @param Request $request 录播链接
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function recLive(Request $request)
    {
        $recUrl = $request->get('url');
        return view('live.rec_live',['recUrl'=>$recUrl]);
    }

}
